package com.cardone.platform.configuration.dao;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import lombok.Getter;
import lombok.Setter;

import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import com.cardone.common.dto.PaginationDto;
import com.cardone.common.util.EntityUtils;
import com.cardone.context.Attributes;
import com.cardone.context.ContextHolder;
import com.cardone.persistent.builder.Model;
import com.cardone.persistent.builder.ModelArgs;
import com.cardone.persistent.builder.ModelUtils;
import com.cardone.persistent.support.JdbcTemplateSupport;
import com.cardone.persistent.support.PaginationArgs;
import com.cardone.platform.configuration.dto.SiteDto;
import com.google.common.collect.Maps;

/**
 * 站
 *
 * @author yaohaitao
 *
 */
@Getter
@Setter
public class SiteJdbcDao implements SiteDao {
	@Override
	public int deleteByIds(final SiteDto deleteSite) {
		final Model model = ModelUtils.newModel();

		if (MapUtils.isNotEmpty(deleteSite.getAttrs())) {
			model.putAll(deleteSite.getAttrs());
		}

		model.put(Attributes.ids.name(), deleteSite.getIds());

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.deleteByIds.id(), model);
	}

	@Override
	public <P> P findByCode(final Class<P> mappedClass, final String code) {
		final Model model = ModelUtils.newModel();

		model.putExtend(null, Attributes.code.name(), null, code);

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findByCodeOrId(final Class<P> mappedClass, final SiteDto findSite) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findSite, Attributes.code.name());

		final Model model = ModelUtils.newModel(new ModelArgs(findSite, whereAndEqProperties));

		if (MapUtils.isNotEmpty(findSite.getAttrs())) {
			model.putAll(findSite.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByCode.id(), model);
	}

	@Override
	public <P> P findById(final Class<P> mappedClass, final SiteDto findSite) {
		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(findSite);

		final Model model = ModelUtils.newModel(new ModelArgs(findSite, whereAndEqProperties).setIsSimple(true));

		if (MapUtils.isNotEmpty(findSite.getAttrs())) {
			model.putAll(findSite.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findById.id(), model);
	}

	@Override
	public <P> P findByServerName(final Class<P> mappedClass, final String serverName, final String projectCode, final String code) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put(Attributes.serverName.name(), serverName);
		model.put(Attributes.projectCode.name(), projectCode);
		model.put(Attributes.code.name(), code);

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findByServerName.id(), model);
	}

	@Override
	public <P> P findBySiteUrlCode(final Class<P> mappedClass, final String siteUrlCode) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put(SiteDto.Attributes.siteUrlCode.name(), siteUrlCode);

		return ContextHolder.getBean(JdbcTemplateSupport.class).find(mappedClass, SqlIds.findBySiteUrlCode.id(), model);
	}

	@Override
	public <P> List<P> findListByLikeCode(final Class<P> mappedClass, final SiteDto findListSite) {
		final Model model = ModelUtils.newModel(new ModelArgs(findListSite).setIsSimple(true));

		if (MapUtils.isNotEmpty(findListSite.getAttrs())) {
			model.putAll(findListSite.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByLikeCode.id(), model);
	}

	@Override
	public <P> List<P> findListByProjectCode(final Class<P> mappedClass, final String projectCode) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put(Attributes.projectCode.name(), projectCode);

		return ContextHolder.getBean(JdbcTemplateSupport.class).findList(mappedClass, SqlIds.findByProjectCode.id(), model);
	}

	@Override
	public int insertByCode(final SiteDto insertSite) {
		if (StringUtils.isBlank(insertSite.getId())) {
			insertSite.setId(UUID.randomUUID().toString());
		}

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.insert.stringValue(), insertSite));

		if (MapUtils.isNotEmpty(insertSite.getAttrs())) {
			model.putAll(insertSite.getAttrs());
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.insertByCode.id(), model);
	}

	@Override
	public <P> PaginationDto<P> paginationByLikeCode(final Class<P> mappedClass, final SiteDto paginationSite) {
		final Model model = ModelUtils.newModel(new ModelArgs(paginationSite).setIsSimple(true));

		if (MapUtils.isNotEmpty(paginationSite.getAttrs())) {
			model.putAll(paginationSite.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).pagination(mappedClass, new PaginationArgs(SqlIds.readByLikeCode.id(), SqlIds.findByLikeCode.id(), paginationSite), model);
	}

	@Override
	public Integer readByCodeNqIdForCount(final SiteDto readSite) {
		final String[] whereAndEqProperties = { Attributes.code.name() };

		final Model model = ModelUtils.newModel(new ModelArgs(readSite, whereAndEqProperties));

		if (MapUtils.isNotEmpty(readSite.getAttrs())) {
			model.putAll(readSite.getAttrs());
		}

		final String[] whereAndNqProperties = EntityUtils.getWhereProperties(readSite);

		if (ArrayUtils.isNotEmpty(whereAndNqProperties)) {
			ModelUtils.put(model, new ModelArgs(Model.Keys.whereAndNq.stringValue(), readSite, whereAndNqProperties));
		}

		model.putTrue(Model.Keys.whereAndBetween.stringValue(), Attributes.sysdate.name());

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(Integer.class, SqlIds.readByCode.id(), model);
	}

	@Override
	public String readIdByCode(final String code) {
		final Map<String, Object> model = Maps.newHashMap();

		model.put(Attributes.code.name(), code);

		return ContextHolder.getBean(JdbcTemplateSupport.class).read(String.class, SqlIds.readIdByCode.id(), model);
	}

	@Override
	public <P> P saveByIdOrCode(final Class<P> mappedClass, final SiteDto saveSite) {
		final P oldSite = this.findByCodeOrId(mappedClass, saveSite);

		if (oldSite == null) {
			this.insertByCode(saveSite);

			return this.findByCodeOrId(mappedClass, saveSite);
		}

		final int updateCount = this.updateByCode(saveSite, oldSite);

		if (updateCount < 1) {
			return oldSite;
		}

		return this.findByCodeOrId(mappedClass, saveSite);
	}

	@Override
	public int updateByCode(final SiteDto updateSite) {
		final SiteDto oldSite = this.findByCodeOrId(SiteDto.class, updateSite);

		return this.updateByCode(updateSite, oldSite);
	}

	private <P> int updateByCode(final SiteDto updateSite, final P oldSite) {
		if (oldSite == null) {
			return 0;
		}

		final String[] useProperties = EntityUtils.addNotNullTimeSegmentProperties(new String[] { Attributes.parentId.name(), Attributes.projectId.name(), Attributes.code.name(), Attributes.name.name() }, updateSite);

		final String[] updateProperties = com.cardone.common.util.BeanUtils.diffProperties(updateSite, oldSite, useProperties);

		if (ArrayUtils.isEmpty(updateProperties)) {
			return 0;
		}

		final String[] whereAndEqProperties = EntityUtils.getWhereProperties(oldSite, Attributes.id.name());

		com.cardone.common.util.BeanUtils.copyProperties(oldSite, updateSite, whereAndEqProperties);

		final Model model = ModelUtils.newModel(new ModelArgs(Model.Keys.update.stringValue(), updateSite, updateProperties), new ModelArgs(updateSite, whereAndEqProperties));

		if (MapUtils.isNotEmpty(updateSite.getAttrs())) {
			model.putAll(updateSite.getAttrs());
		}

		return ContextHolder.getBean(JdbcTemplateSupport.class).update(SqlIds.updateByCode.id(), model);
	}
}